import createHandler from './index'
import {selectOne} from "./fake-db"

/**
 *  code: 5000-6000
 */


const findItemById = createHandler(id => {
    let item = selectOne('item', 'id', id)
    if (!item) return {
        code: 5001,
        data: '商品不存在'
    }
    const category = selectOne('category', 'id', item.category)
    item = {...item, category: category ? category.name : '未知类别'}
    return {
        code: 0,
        data: item
    }
})

const getOption = createHandler((itemId, optionKey) => {
    const item = selectOne('item', 'id', itemId)
    if (!item) return {
        code: 5001,
        data: '商品不存在'
    }
    const option = item.options.find(option => option.key === optionKey)
    if (!option) return {
        code: 5002,
        msg: '配置不存在'
    }
    return {
        code: 0,
        data: {
            label: item.label,
            option: option
        }
    }
})

export {findItemById, getOption}